import type { Response } from "express";
import { logger, CODE } from "@/utils/log";

// !报错处理
export const handleError = (
  res: Response,
  err: string | unknown,
  code?: number
) => {
  logger.error(CODE.business, err);
  res.status(200).json({
    code: code || 500,
    msg: err,
    data: {},
  });
};

// !成功处理
export const handleSucceed = (
  res: Response,
  data: any,
  msg: string = "操作成功",
  code?: number
) => {
  logger.info(CODE.business, msg);

  res.status(200).json({
    code: code || 200,
    msg,
    data,
  });
};
